<?php

define('table','product_class');

class product_categoryControlPage extends commonControlPage {

    public function categoryModule() {
        $category = new class_category();
        $this->view->lists = $category->doList(table);
        $act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
        call_user_func(array($this,$act.'Action'));
    }

	/*[列表]*/
    private function listAction() 
	{
		 if (POST && AJAX) 
		 {
			if($_POST['del']=='all')
			{
				DeteteTable(table," 1=1 ");
			}
			$this->outputJson(1,'操作成功');
		 }
		$this->view->view_act = ' class="active" '; 
        $this->showMainPage('admincp/'.table.'_list');
    }

	/*[添加]*/
	private function addAction() {
        if (POST && AJAX) {
			$data = $this->GetValidateData();     //得到验证后的数据  

            $category = new class_category();
            if ($category->add(table,$data['class_name'],$data['parent_id'],$data['sort'])) 
			{
                class_cache::delete('product_class');
                $this->outputJson(1,'操作成功');
            } 
			else 
			{
                $this->outputJson(0,'操作失败，请检查输入');
            }
        }
		$this->view->add_act = ' class="active" '; 
        $this->showMainPage('admincp/'.table.'_modi');
    }

	/*[编辑]*/
    private function modiAction() 
	{
        $class_id = $_GET['class_id'];
        $class = $_ENV['db']->fetch("SELECT * FROM ".table." WHERE `class_id` = '{$class_id}'");
        if (!$class) $this->showMessage('指定的分类不存在',AJAX ? '0' : 'back');

        if (POST && AJAX) 
		{
            $data = $this->GetValidateData();     //得到验证后的数据 

            $category = new class_category();
            
            if ($category->modi(table,$class_id,$data['class_name'],$data['parent_id'],$data['sort'])) {
                class_cache::delete('product_class');
                $this->outputJson(1,'操作成功');
            } else {
                $this->outputJson(0,'操作失败，请检查输入');
            }
        }
		$this->view->edit_act = ' class="active" '; 
        $this->view->class = $class;
        $this->showMainPage('admincp/'.table.'_modi');
    }

	/*[删除]*/
	private function delAction()
	{
		$class_id = $_GET['class_id'];
		$category = new class_category();
		$isHave = $category->IsHaveChild(table,$class_id);
		if(!$isHave)
		{
			$_ENV['db']->runSql("DELETE FROM `".table."` WHERE `class_id` = '{$class_id}'");
			$this->showMessage("删除成功",'back');
		}
		$this->showMessage("此节点有叶子节点，不能删除",'back');
		
	}

	/*[验证POST过来的数据]*/
	private function GetValidateData(){
		$columns = array('class_name','parent_id','sort');
        $data = getRequests($columns);
        $data['parent_id'] = $data['parent_id'] ? $data['parent_id'] : '0';
	    $data['sort'] = $data['sort'] ? $data['sort'] : 0;
        if (!$data['class_name']) {
        	$this->outputJson(0,'请填写分类名称');
         }
        if (!preg_match('/[0-9]+/',$data['parent_id']) || ($data['parent_id'] && !$_ENV['db']->fetchCol("SELECT class_id FROM ".table." WHERE class_id = '{$data['parent_id']}'"))) {
             $this->outputJson(0,'指定的上级分类不存在');
         }
         if (!preg_match('/[0-9]+/',$data['sort'])) {
             $this->outputJson(0,'排序必须为数字');
         }
		 return $data;
	}
	
	
	/*[导入]*/
	public function importModule()
	{
		if($_GET['opt']=='start')
		{
			header("Content-type:text/html;charset=utf-8");
			if(!$_FILES)
			{
				$this->showMessage('请选择导入文件!','back');		
			}
			$data = class_excel::ImportCsv();
			$_ENV['db']->beginTransaction();
			
			if($data)
			{
				
				$parentArr = array();    //父级集合
				$childArr = array();     //子级集合
				foreach($data as $key=>$value)
				{
					if($key==0)
					{//标头跳过
						continue;
					}
					$array = explode(",",trim($value));
											
					$tempCode =  FormatStr($array['1']);
					$arrids   =  explode(',',$tempCode);
					if(count($arrids )==1)
					{   //父级
						$resultID = InsertTable(table,array(
												'parent_id'=>0,
												'class_name'=>$array[2],
												'path'=>0,
												'code'=>$array[1],
												'sort'=>0
												));
						$array['id'] = $resultID;
						$parentArr[intval($array['1'])] = $array;
					}
					else
					{   //自级
						$array ['pid'] = $arrids[count($arrids)-2];
						$childArr[intval($array['1'])] = $array;
					}
				}
				
				foreach($childArr as $ckey => $cvalue )
				{
					$parentItem = $parentArr[$cvalue['pid']];
					if($parentItem)
					{
						$resultID = InsertTable(table,array(
												'parent_id'=>$parentItem['id'],
												'class_name'=>$cvalue[2],
												'path'=>'0,'. $parentItem['id'],
												'code'=> $cvalue[1],//$parentItem[1].$cvalue[1],
												'sort'=>0
												));
					}
				}
				
			}
			else
			{
				$_ENV['db']->rollback();
				$this->showMessage('导入失败!','back');	
			}
			
			$_ENV['db']->commit();
			$this->showMessage('导入成功!','back');	
		}
		
		$this->view->import_act = ' class="active" '; 
        $this->showMainPage('admincp/product_class_import');
	}
	
	
	
	
	
}